package com.spring.redis.operation.config;

import com.spring.redis.operation.serializer.FstRedisSerializer;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.FstCodec;
import org.redisson.config.Config;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.IOException;

/**
 * redis 缓存配置，仅当配置文件中spring.cache.type = redis时生效
 *
 * @author zhouyu
 */
@Configuration
public class RedisCacheConfig {


    @Bean(destroyMethod = "shutdown", name = "redissonClient")
    public RedissonClient redissonClient(@Value("${spring.redis.redisson.config}") Resource configFile) throws IOException {
        Config config = Config.fromYAML(configFile.getInputStream());
        config.setCodec(new FstCodec());
        return Redisson.create(config);
    }


    @Bean(name = "redissonConnectionFactory")
    public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redissonClient) {
        return new RedissonConnectionFactory(redissonClient);
    }

    /**
     * redisTemplate 相关配置
     *
     * @param redissonConnectionFactory redissonConnectionFactory
     * @return org.springframework.data.redis.core.RedisTemplate
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedissonConnectionFactory redissonConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redissonConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new FstRedisSerializer());
        redisTemplate.setHashValueSerializer(new FstRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


    /**
     * stringRedisTemplate 相关配置
     *
     * @param redissonConnectionFactory redissonConnectionFactory
     * @return org.springframework.data.redis.core.RedisTemplate
     */
    @Bean
    public RedisTemplate<String, String> stringRedisTemplate(RedissonConnectionFactory redissonConnectionFactory) {
        RedisTemplate<String, String> stringRedisTemplate = new RedisTemplate<>();
        stringRedisTemplate.setConnectionFactory(redissonConnectionFactory);
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
        stringRedisTemplate.afterPropertiesSet();
        return stringRedisTemplate;
    }


    /**
     * 对hash类型的数据操作
     *
     * @param redisTemplate redisTemplate
     * @return org.springframework.data.redis.core.HashOperations
     */
    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    /**
     * 对redis字符串类型数据操作
     *
     * @param redisTemplate redisTemplate
     * @return org.springframework.data.redis.core.ValueOperations
     */
    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    /**
     * 对redis字符串类型数据操作
     *
     * @param stringRedisTemplate redisTemplate
     * @return org.springframework.data.redis.core.ValueOperations
     */
    @Bean
    public ValueOperations<String, String> stringValueOperations(RedisTemplate<String, String> stringRedisTemplate) {
        return stringRedisTemplate.opsForValue();
    }

    /**
     * 对链表类型的数据操作
     *
     * @param redisTemplate redisTemplate
     * @return org.springframework.data.redis.core.ListOperations
     */
    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    /**
     * 对无序集合类型的数据操作
     *
     * @param redisTemplate redisTemplate
     * @return org.springframework.data.redis.core.SetOperations
     */
    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    /**
     * 对有序集合类型的数据操作
     *
     * @param redisTemplate redisTemplate
     * @return org.springframework.data.redis.core.ZSetOperations
     */
    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }

}
